#include "MyMongoC.h"
#include <iostream>
#include <pthread.h>


void Insert(MyMongoC& mmc)
{
    //bson
    bson_t* command = BCON_NEW("string", "123");
    BCON_APPEND(command, "int32", BCON_INT32(33333));
    BCON_APPEND(command, "int64", BCON_INT64(33333));
    BCON_APPEND(command, "double", BCON_DOUBLE(33333.33333));
    BCON_APPEND(command, "bool", BCON_BOOL(true));
    
    //添加子文档
    bson_t* child;
    BSON_APPEND_DOCUMENT_BEGIN(command, "child", child);
    BSON_APPEND_UTF8(child, "是UTF8", "方式一");
    BSON_APPEND_INT32(child, "子INT32", 213);
    bson_append_document_end(command, child);

    //JSON形式打印
    char* str = bson_as_json (command, NULL);
    std::cout << str << std::endl;

    //插入
    if(mmc.Insert("t1", command) == false)
    {
        std::cout << "Insert err" << std::endl;
        return;
    }

    bson_destroy (child);
    bson_free (str);
    bson_destroy(command);

}

void Query(MyMongoC& mmc)
{
    //查询条件
    bson_t* conditions = bson_new();
    BSON_APPEND_UTF8(conditions, "name", "zhangsan");

    //显示条件
    bson_t* query = bson_new();
    BSON_APPEND_INT32(query, "_id", 1);
    BSON_APPEND_INT32(query, "name", 1);
    BSON_APPEND_INT32(query, "int32", 1);

    //查询
    mongoc_cursor_t* cursor = mmc.Query("t1", conditions, query);
    if(cursor == NULL)
    {
        bson_destroy(query);
        bson_destroy(conditions);
        return;
    }

    //遍历
    const bson_t* doc;
    while (mongoc_cursor_next (cursor, &doc)) 
    {
        //转json
        char* str = bson_as_json (doc, NULL);
        //显示
        printf ("%s\n", str);
        //释放
        bson_free (str);
    }

    bson_destroy(query);
    bson_destroy(conditions);
    mongoc_cursor_destroy(cursor);
}

void Update(MyMongoC& mmc)
{
    //查询条件
    bson_t* conditions = bson_new();
    BSON_APPEND_UTF8(conditions, "name", "lisi");

    //更新内容
    bson_t* update = BCON_NEW("$set", "{", 
        "name", BCON_UTF8("zhangsan"),
    "}");

    if(mmc.Update("t1", conditions, update) == false)
    {
        std::cout << "Update err" << std::endl;
        bson_destroy(update);
        bson_destroy(conditions);
        return;
    }

    bson_destroy(update);
    bson_destroy(conditions);
}

void Delete(MyMongoC& mmc)
{
    //查询条件
    bson_t* conditions = bson_new();
    BSON_APPEND_UTF8(conditions, "hello", "123");

    //删除
    if(mmc.Delete("t1", conditions) == false)
    {
        std::cout << "Delete err" << std::endl;
        bson_destroy(conditions);
        return;
    }

    bson_destroy(conditions);
}

void Count(MyMongoC& mmc)
{
    //查询条件
    bson_t* conditions = bson_new();
    BSON_APPEND_UTF8(conditions, "hello", "123");

    //删除
    int64_t nCount = mmc.Count("t1", conditions);
    if(nCount == -1)
    {
        std::cout << "Count err" << std::endl;
        bson_destroy(conditions);
        return;
    }
    
    std::cout << nCount << std::endl;

    bson_destroy(conditions);
}

int main()
{
    MyMongoC mmc;

    //连接
    if(mmc.Connect("127.0.0.1", 27017, "test", "test", "123456") == false)
    {
        std::cout << "Connect err" << std::endl;
        return -1;
    }

    //Insert(mmc);
    //Query(mmc);
    //Update(mmc);
    //Delete(mmc);
    Count(mmc);

    return 0;
}